﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class ProcessController : Controller
    {
        //
        // GET: /Process/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSQTCongTac
                            from cv in context.DSChucVu
                            from mn in context.DSMaNgach
                            where dt.IDCanBo == id && dt.IDChucVu == cv.ID && dt.IDMaNgach == mn.ID
                            select new { dt.TuNgay, dt.DenNgay, dt.CoQuan, dt.ID,dt.BacLuong, 
                                dt.HeSoLuongTong,dt.GhiChu, dt.IsThongKe, TenChucVu = cv.Ten, mn.TenNgach } into abc
                                orderby abc.TuNgay ascending
                                select abc).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.TuNgay,
                                 customer.DenNgay,
                                  customer.CoQuan,
                                 customer.BacLuong,
                                 customer.HeSoLuongTong,
                                 customer.GhiChu,
                                 customer.IsThongKe,
                                 customer.TenChucVu,
                                 customer.TenNgach
                             } );
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id = 0)
        {
            List<QTCongTac> dsqhgd = QuaTrinhCongTacModels.GetQTCongTacTheoCanBo(id);
            ViewBag.DSQTCongTac = dsqhgd;
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View(dsqhgd);
        }
        public ActionResult Add(int id = 0)
        {
            List<ChucVu> dsll = PosModels.GetDSChucVu();
            ViewBag.DSChucVu = dsll;
            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            QTCongTac themKyLuat = new QTCongTac();
            themKyLuat.CoQuan = form["CoQuan"];
            themKyLuat.IDChucVu = Convert.ToInt32(form["IDChucVu"]);
            themKyLuat.TuNgay = DateTime.Parse(form["TuNgay"],ci.DateTimeFormat);
            
            
            //xu lý tang ngay nang luong
            BangMaNgach ma_ngach = MaNgachModels.GetChiTietMaNgachMaNgach(form["MaNgachCC"]);
            BangNhomNgach nhom_ngach = NhomNgachModels.GetChiTietNhomNgach(ma_ngach.IDNhomNgach);
            if (form["DenNgay"] == "")
            {
                
                int dday = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Day;
                int mmon = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Month;
                int yyear = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Year;
                yyear += (int)nhom_ngach.SoNamTangLuong;
                DateTime _dt = new DateTime(yyear, mmon, dday);
                themKyLuat.DenNgay = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
            }
            else
            {
                themKyLuat.DenNgay = DateTime.Parse(form["DenNgay"], ci.DateTimeFormat);
            }
            
            themKyLuat.IDMaNgach = ma_ngach.ID;
            themKyLuat.BacLuong = Convert.ToInt32(form["BacLuong"]);
            themKyLuat.HeSoLuongTong = nhom_ngach.HeSoGoc;
            themKyLuat.GhiChu = form["GhiChu"];
            bool isChecked = (form["IsThongKe"] != "false");
            themKyLuat.IsThongKe = isChecked;

            themKyLuat.IDCanBo = id;
            //themQHGD.IDCanBo = id;
            Double hesoluongtong = nhom_ngach.HeSoGoc+(((Convert.ToInt32(form["BacLuong"])-1)*nhom_ngach.HeSoTang));
            QuaTrinhCongTacModels.Add(themKyLuat);
            tblDienBienLuong canbodienbienluong = DienBienLuongModels.GetThongTinCanBo(id);
            if (canbodienbienluong.HeSoLuongTong != hesoluongtong)
            {
                tblDienBienLuong dbluong = new tblDienBienLuong();
                dbluong.IDCanBo = id;
                dbluong.IDChucVu = Convert.ToInt32(form["IDChucVu"]);
                dbluong.TuNgay = DateTime.Parse(form["TuNgay"], ci.DateTimeFormat);
                if (form["DenNgay"] == "")
                {

                    int dday = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Day;
                    int mmon = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Month;
                    int yyear = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Year;
                    yyear += (int)nhom_ngach.SoNamTangLuong;
                    DateTime _dt = new DateTime(yyear, mmon, dday);
                    dbluong.DenNgay = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
                    dbluong.NgayTangLuong = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
                }
                else
                {
                    dbluong.DenNgay = DateTime.Parse(form["DenNgay"], ci.DateTimeFormat);
                    dbluong.NgayTangLuong = DateTime.Parse(form["DenNgay"], ci.DateTimeFormat);
                }
                dbluong.IDMaNgach = ma_ngach.ID;
                dbluong.BacLuong = Convert.ToInt32(form["BacLuong"]);
                dbluong.HeSoLuong = nhom_ngach.HeSoGoc;
                dbluong.HeSoLuongTong = hesoluongtong;
                DienBienLuongModels.Add(dbluong);

            }
            //return RedirectToAction("Index");
            return RedirectToAction("Index", "Process", new { id = themKyLuat.IDCanBo });
        }

        public ActionResult Update(int id)
        {
            List<ChucVu> dsll = PosModels.GetDSChucVu();
            ViewBag.DSChucVu = dsll;            

            QTCongTac listQHGD = QuaTrinhCongTacModels.GetQTCongTac(id);
            ViewBag.DSQTCongTac = listQHGD;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            QTCongTac s = QuaTrinhCongTacModels.GetQTCongTac(id);
            s.CoQuan = form["CoQuan"];//s.IDCanBo = Convert.ToInt32(form["IDCanBo"]);
            s.IDChucVu = Convert.ToInt32(form["IDChucVu"]);
            s.TuNgay = DateTime.Parse(form["TuNgay"],ci.DateTimeFormat);
            s.DenNgay = DateTime.Parse(form["DenNgay"],ci.DateTimeFormat);
            //xu lý tang ngay nang luong
            BangMaNgach ma_ngach = MaNgachModels.GetChiTietMaNgachMaNgach(form["MaNgachCC"].ToString().Trim());
            BangNhomNgach nhom_ngach = NhomNgachModels.GetChiTietNhomNgach(ma_ngach.IDNhomNgach);
            if (form["DenNgay"] == "")
            {
                
                int dday = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Day;
                int mmon = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Month;
                int yyear = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Year;
                yyear += (int)nhom_ngach.SoNamTangLuong;
                DateTime _dt = new DateTime(yyear, mmon, dday);
                s.DenNgay = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
            }
            else
            {
                s.DenNgay = DateTime.Parse(form["DenNgay"], ci.DateTimeFormat);
            }
            s.IDMaNgach = ma_ngach.ID;
            s.BacLuong = Convert.ToInt32(form["BacLuong"]);
            s.GhiChu = form["GhiChu"];
            bool isChecked = (form["IsThongKe"] != "false");
            s.IsThongKe = isChecked;
            tblDienBienLuong dbluong = DienBienLuongModels.GetDienBienLuongTheoNgay(s.IDCanBo,DateTime.Parse(form["TuNgay"],ci.DateTimeFormat));
            Double hesoluongtong = nhom_ngach.HeSoGoc + (((Convert.ToInt32(form["BacLuong"]) - 1) * nhom_ngach.HeSoTang));
            if (dbluong != null)
            {
                dbluong.IDCanBo = s.IDCanBo;
                dbluong.IDChucVu = Convert.ToInt32(form["IDChucVu"]);
                dbluong.TuNgay = DateTime.Parse(form["TuNgay"], ci.DateTimeFormat);
                if (form["DenNgay"] == "")
                {

                    int dday = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Day;
                    int mmon = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Month;
                    int yyear = DateTime.Parse((form["TuNgay"] == "" ? DateTime.Now.ToString("dd/MM/yyyy") : form["TuNgay"]), ci.DateTimeFormat).Year;
                    yyear += (int)nhom_ngach.SoNamTangLuong;
                    DateTime _dt = new DateTime(yyear, mmon, dday);
                    dbluong.DenNgay = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
                    dbluong.NgayTangLuong = DateTime.Parse(_dt.ToString("dd/MM/yyyy"), ci.DateTimeFormat);
                }
                else
                {
                    dbluong.DenNgay = DateTime.Parse(form["DenNgay"], ci.DateTimeFormat);
                    dbluong.NgayTangLuong = DateTime.Parse(form["DenNgay"], ci.DateTimeFormat);
                }
                dbluong.IDMaNgach = ma_ngach.ID;
                dbluong.BacLuong = Convert.ToInt32(form["BacLuong"]);
                dbluong.HeSoLuong = nhom_ngach.HeSoGoc;
                dbluong.HeSoLuongTong = hesoluongtong;
                DienBienLuongModels.Update(dbluong);
            }
            QuaTrinhCongTacModels.Update(s);
            return RedirectToAction("Index", "Process", new { id = s.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            QTCongTac s = QuaTrinhCongTacModels.GetQTCongTac(id);
            QuaTrinhCongTacModels.Delete(id);

            return RedirectToAction("Index", "Process", new { id = s.IDCanBo });
        }

        public JsonResult GetDSTenNgachCC()
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var kq = (from ncc in context.DSMaNgach select ncc);            
            Dictionary<string, string> data = new Dictionary<string, string>();
            foreach (var l in kq)
            {
                data.Add(l.MaNgach, l.TenNgach);
            }
            return Json(data.ToArray(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetDSMaNgachTheoTen(String tenngach)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var kq = (from ncc in context.DSMaNgach where ncc.TenNgach == tenngach select ncc);
            Dictionary<string, string> data = new Dictionary<string, string>();
            foreach (var l in kq)
            {
                data.Add(l.MaNgach, l.TenNgach);
            }
            return Json(data.ToArray(), JsonRequestBehavior.AllowGet);
        }

       
    }
}
